package com.cqh.recruit.config;

import com.cqh.recruit.interceptor.TokenInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author zxd
 * @date 2025/6/7
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    //配置拦截器交给Spring容器进行管理
    @Bean
    public TokenInterceptor authInterceptor(){
        return new TokenInterceptor();
    }

    //注册拦截器
    public void addInterceptors(InterceptorRegistry registry) {
        // 临时注释掉拦截器注册以便测试
        registry.addInterceptor(authInterceptor()).addPathPatterns("/**")
                //配置不拦截swagger资源和静态资源
                .excludePathPatterns("/api/student/stuLogin/**","/api/school/empLogin/**","/api/upload/image",
                        "/images/**",
                        "/swagger-ui/**",// Swagger UI 页面
                        "/v3/api-docs",           // OpenAPI 文档端点
                        "/v3/api-docs/**",        // OpenAPI 文档子路径
                        "/swagger-resources/**",  // Swagger 资源文件
                        "/webjars/**" );
    }

    /**
     * 图片访问路径配置
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // images 是访问图片资源的前缀，比如要访问test1.png
        // 则全路径为：http://localhost:端口号/images/test1.png
        registry.addResourceHandler("/images/**")
                //此处为设置服务端存储图片的路径（前端上传到后台的图片保存位置）file:代表绝对路径
                .addResourceLocations("file:E:\\image");
    }
}